package com.init.iblog.Controller;

import com.init.iblog.domain.ResponseResult;
import com.init.iblog.domain.dto.UserDto;
import com.init.iblog.domain.entity.Role;
import com.init.iblog.domain.entity.User;
import com.init.iblog.domain.vo.PageVo;
import com.init.iblog.service.RoleService;
import com.init.iblog.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/system/user")
public class UserController {


    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    /**
     * 查询用户列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    public ResponseResult<PageVo> getUserList(Integer pageNum, Integer pageSize, UserDto userDto){
        System.out.println("=============进入查询用列表==========");
        return userService.getUserList(pageNum, pageSize,userDto);
    }


    /**
     * 新增用户
     * 用户名必须唯一
     * @param user
     * @return
     */
    @PostMapping
    public ResponseResult addUser(@RequestBody User user){
        System.out.println("=========进入新增用户接口==========");
        user.setCreateTime(new Date());
        return userService.addUser(user);
    }

    /**
     * 删除用户
     * 逻辑删除
     * @return
     */
    @DeleteMapping("/{id}")
    public ResponseResult delUser(@PathVariable List<Long> id){
        System.out.println("========进入删除用户接口==========");
        return userService.deleteUser(id);
    }


    /**
     * 用户密码重置
     * @return
     */
    @PutMapping("/resetPwd")
    public ResponseResult resetUserPwd(@PathVariable String passward){


        return userService.resetUserPwd(passward);
    }


    /**
     * 更新用户
     * @return
     */
    @PutMapping
    public ResponseResult updateUser(@RequestBody User user){
        System.out.println("==========进入修改用户信息接口==========");

        return userService.updateUser(user);
    }


    /**
     * 修改用户状态
     *
     TODO  存在问题这里
     * @return
     */
    @PutMapping("/changeStatus")
    public ResponseResult changeUserStatus(@PathVariable Long id,@PathVariable String status){
        System.out.println("==========进入修改用户状态接口==========");

        return userService.changeUserStatus(id,status);
    }


    /**
     * 获取用户信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseResult getUser(@PathVariable Long id){

        System.out.println("=========进入用户信息接口=============");

        User user = userService.getById(id);
        roleService.list();
        return ResponseResult.okResult(user);
    }
}
